package com.sixetc.sixcontroller.slice;

import ohos.rpc.*;

// 以计算加法为例，对端实现的客户端如下
public class MyRemote extends RemoteObject implements IRemoteBroker {
    private static final int ERR_OK = 0;
    private static final int ERROR = -1;
    private static final int COMMAND_PLUS = IRemoteObject.MIN_TRANSACTION_ID;
    private static final String DESCRIPTOR = "com.XXX.DESCRIPTOR";

    public MyRemote() {
        super("MyService_Remote");
    }

    @Override
    public IRemoteObject asObject() {
        return this;
    }

    @Override
    public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
        String token = data.readInterfaceToken();
        if (!DESCRIPTOR.equals(token)) {
            reply.writeInt(ERROR);
            return false;
        }

        if (code != COMMAND_PLUS) {
            reply.writeInt(ERROR);
            return false;
        }

        int value1 = data.readInt();
        int value2 = data.readInt();
        int sum = value1 + value2;
        reply.writeInt(ERR_OK);
        reply.writeInt(sum);
        return true;
    }
    
}
